/**
 *
 * @author John Creecy
 */
package battlebarnserver;

import java.util.Random;

import battlebarnserver.entities.PlayerEntity;
import battlebarnserver.entities.ProjectileEntity;
import battlebarnserver.util.Constants;
import battlebarnserver.util.Conversions;

public class ProcessorThread implements Runnable {

	private String packetData;
	private String sourceIp;
	private int action; // 0 none, 1 up, 2 right, 3 down, 4 left
	private int movementUnits;
	private int tileX;
	private int tileY;
	
	private int tileX2;
	private int tileY2;
	private int shot;
	private int facing;
	private int health;
	private boolean dead;


	public ProcessorThread(String packetDataString, String ipAddress, int port) {
		this.packetData = packetDataString;
		this.sourceIp = ipAddress;
		movementUnits = 1;
		shot = 0;
	}

	public void run() {
		processPacketData();
	}

	private void processPacketData() {
		Random generator;
		if (BattleBarn.actionStack.containsKey(sourceIp)) {
			if (BattleBarn.actionStack.get(sourceIp).getType().equals("Player")) {
				PlayerEntity previousBaseEntity = (PlayerEntity) BattleBarn.actionStack.get(sourceIp);
				tileX = previousBaseEntity.getTileX();
				tileY = previousBaseEntity.getTileY();
				
				tileX2 = previousBaseEntity.getTileX();
				tileY2 = previousBaseEntity.getTileY();
				facing = previousBaseEntity.getFacing();
				dead = previousBaseEntity.isPlayerDead();
				health = previousBaseEntity.getHealth();
			}
		} else {
			// Spawn new user in random location
			generator = new Random();
			tileX = generator.nextInt(Constants.TILE_MAX_X);
			tileY = generator.nextInt(Constants.TILE_MAX_Y);
			facing = 2;
			health = Constants.STARTING_PLAYER_HEALTH;
		}

		if (dead == false) {
			action = Integer.parseInt(this.packetData.substring(0, 1));

			// 0 none, 1 up, 2 right, 3 down, 4 left
			switch (action) {
			case 1:
				tileY = (tileY - movementUnits);
				break;
			case 2:
				tileX = (tileX + movementUnits);
				break;
			case 3:
				tileY = (tileY + movementUnits);
				break;
			case 4:
				tileX = (tileX - movementUnits);
				break;
			case 5:
				shot = 1;
			default:
				break;
			}

			// new projectile
			if (shot == 1) {
				ProjectileEntity projectileEntity = new ProjectileEntity(sourceIp, Conversions.getVelocityX(facing), Conversions.getVelocityY(facing), Conversions.getBoundsX(tileX), Conversions.getBoundsX(tileY));
				BattleBarn.removeProjectiles.remove(sourceIp);
				BattleBarn.activeProjectiles.add(projectileEntity);
				BattleBarn.gameState.add(projectileEntity);
			// movement command
			} else if (Conversions.collisionTest(tileX, tileY)) {
				PlayerEntity playerEntity = new PlayerEntity(sourceIp, health, Conversions.getBoundsX(tileX), Conversions.getBoundsY(tileY), action, false, true);
				BattleBarn.actionStack.put(sourceIp, playerEntity);
			// direction change only
			} else if (!Conversions.collisionTest(tileX, tileY) && action != 0) {
				PlayerEntity playerEntity = new PlayerEntity(sourceIp, health, Conversions.getBoundsX(tileX2), Conversions.getBoundsY(tileY2), action, false, false);
				BattleBarn.actionStack.put(sourceIp, playerEntity);
			}
		}
	}
}
